package com.besttone.action;

import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

import com.besttone.bean.BatchImportDetailBean;
import com.besttone.util.JsonUtil;
import com.besttone.util.Sms2SolrUtil;
import com.besttone.util.SolrObject;
import com.jfinal.core.Controller;

public class SolrOutputAction extends Controller {
    private static Log _log = LogFactory.getLog(ShortlinkAction.class);

    public static void writeTxt(String fileName, String content) {

	FileWriter fw = null;
	try {
	    fileName = "E:\\" + fileName + ".txt";
	    _log.info("文件名称:" + fileName);
	    fw = new FileWriter(fileName);
	    fw.write(content);
	    fw.write("\r\n");// 写入换行
	    fw.close();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }

    public static void main(String[] args) throws IOException {

	// }
	// public void outPutSolr() {

	StringBuffer strBuf = new StringBuffer();
	strBuf.append("send_parent_account: bstjm_gj ");
	strBuf.append(" AND send_time:[* TO 2016-09-01]");
	_log.info("solr查询语句:" + strBuf.toString());
	int per_page_size = 10000;
	int current_page = 1;
	while (true) {
	    SolrDocumentList docList = Sms2SolrUtil.search(strBuf.toString(),
		    Integer.valueOf(current_page),
		    Integer.valueOf(per_page_size));
	    if (docList.isEmpty()) {
		break;
	    }
	    // StringBuffer lineBuf = new StringBuffer();
	    FileWriter fw = null;
	    String fileName = "sms" + current_page;
	    try {
		fileName = "E:\\" + fileName + ".txt";
		_log.info("文件名称:" + fileName);
		fw = new FileWriter(fileName);

		// fw.close();
	    } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	    String jsonStr = null;
	    Object bean = null;
	    for (SolrDocument doc : docList) {
		bean = SolrObject.toBean(doc, BatchImportDetailBean.class);
		jsonStr = JsonUtil.ObjectToJsonString(bean);
		// System.out.println(jsonStr);
		try {
		    fw.write(jsonStr);
		    fw.write("\r\n");// 写入换行
		} catch (IOException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
	    }
	    fw.close();
	    // writeTxt("sms" + current_page, jsonStr);
	    current_page++;

	}

    }
}
